package normalsys.demo.base.exp;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * Project Name: ms
 * File Name: ExceptionAdvice
 * Package Name: normalsys.demo.base.exp
 * Date: 2018/6/25  11:57
 * Copyright (c) 2018, tianyul All Rights Reserved.
 */
@ControllerAdvice
public class ExceptionAdvice {
    /**
     * 全局异常捕捉处理
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public Map errorHandler(Exception ex) {
        Map map = new HashMap();
        map.put("code", 500);
        map.put("msg", ex.getMessage());
        return map;
    }

    /**
     * 拦截捕捉自定义异常 BizException.class
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = BizException.class)
    public Map myErrorHandler(BizException ex) {
        Map map = new HashMap();
        map.put("code", ex.getCode());
        map.put("msg", ex.getErrorMsg());
        return map;
    }
}
